//
//  CultureToolBar.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/7/29.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "CultureToolBar.h"
#import "SelectButton.h"
#import "MenuButton.h"//为了记录模型用的

@interface CultureToolBar()
{
    BOOL isShowSecond;
}
@property (nonatomic,strong) NSArray *firstTitles;
@property (nonatomic,strong) NSMutableArray *secondTitles;
@property (nonatomic,weak) SelectButton *firstSelectBtn;
@property (nonatomic,strong) UIView *selectView;//二级菜单

@end
@implementation CultureToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        isShowSecond = NO;
        float btnW = ScreenW/3.0;
        self.firstTitles = @[@"红酒知识",@"红酒品鉴",@"红酒时尚"];
        for (NSInteger i=0; i<3; i++) {
            SelectButton * btn = [[SelectButton alloc]initWithFrame:CGRectMake(btnW*i, 0, btnW, 35)];
            [btn setTitle:self.firstTitles[i] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:16];
            btn.backgroundColor = WineColor;
            [btn addTarget:self action:@selector(firstSelectClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
            [self addSubview:btn];
        }
    }
    return self;
}
- (void)firstSelectClick:(SelectButton *)btn
{
    btn.selected = !btn.selected;
    if (btn.selected) {
        if (self.firstSelectBtn!=btn) {
            self.firstSelectBtn.selected = NO;
            self.firstSelectBtn = btn;
        }
        
        float btnW = ScreenW/self.firstTitles.count;
        NSInteger index = btn.x/btnW;
        NSArray *select = self.secondTitles[index];//选中的第一级菜单的数组
        self.selectView.frame = CGRectMake(btnW*index, CGRectGetMaxY(btn.frame), btnW, select.count*25);
        [self.selectView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        for (NSInteger i=0; i<select.count; i++) {
            MenuButton *btn = [[MenuButton alloc]initWithFrame:CGRectMake(0, 25*i, ScreenW/3.0, 25)];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor clearColor];
            [btn addTarget:self action:@selector(secondSelectClick:) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = Font(15);
            [_selectView addSubview:btn];
            MenuListModel *model = select[i];
            [btn setTitle:model.name forState:UIControlStateNormal];
            btn.model = model;
        }
        
        self.selectView.height = 0;
        [self addSubview:self.selectView];
        isShowSecond = YES;
        [UIView animateWithDuration:0.25 animations:^{
            self.selectView.height = 25*select.count;
        }];
    }else{
        isShowSecond = NO;
        [UIView animateWithDuration:0.25 animations:^{
            self.selectView.height = 0;
        } completion:^(BOOL finished) {
            [self.selectView removeFromSuperview];
        }];
    }
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (isShowSecond) {
        CGPoint senondPoint = [self convertPoint:point toView:self.selectView];
        if ([self.selectView pointInside:senondPoint withEvent:event]) {
            //遍历subView找出点击点在哪个button上
            for (UIButton *btn in self.selectView.subviews) {
                CGPoint btnPoint = [self convertPoint:point toView:btn];
                if ([btn pointInside:btnPoint withEvent:event]) {
                    return btn;
                }
            }
            return self.selectView;
        }else{
            return [super hitTest:point withEvent:event];
        }
    }else{
        return [super hitTest:point withEvent:event];
    }
}
- (void)secondSelectClick:(MenuButton *)btn
{
    self.firstSelectBtn.selected = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.selectView.height = 0;
    } completion:^(BOOL finished) {
        [self.selectView removeFromSuperview];
    }];
    if ([self.delegate respondsToSelector:@selector(cultureToolBarClick:)]) {
        [self.delegate cultureToolBarClick:btn.model];
    }
}
- (UIView *)selectView
{
    if (!_selectView) {
        _selectView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW/self.firstTitles.count, 100)];
        _selectView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        _selectView.clipsToBounds = YES;
    }
    return _selectView;
}
- (NSMutableArray *)secondTitles
{
    if (!_secondTitles) {
        _secondTitles = [NSMutableArray array];
    }
    return _secondTitles;
}
- (void)setListDict:(NSDictionary *)listDict
{
    _listDict = listDict;
    for (NSInteger i=0; i<self.firstTitles.count; i++) {//顺序按照firstTitles定死
        [self.secondTitles addObject:listDict[self.firstTitles[i]]];
    }
}



@end
